package com.wangmeng.book.server.action;

import com.wangmeng.book.server.Command;
import com.wangmeng.book.server.CommandResult;
import com.wangmeng.book.utils.ConstValueUtils;
import com.wangmeng.book.utils.SpringContextManager;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: 王猛
 * Date: 13-10-14
 */
public class BookPublishAction implements IBaseAction {
    private static final Logger logger = Logger.getLogger(BookPublishAction.class);

    @Override
    public CommandResult execute(Command cmd) {
        CommandResult cr = new CommandResult();
        cr.setCode(CommandResult.CODE_COMMON_ERROR);
        int rowNum = 0;
        try {
            Map<String, Object> params = cmd.getData();
            if (params != null) {
                Integer opType = (Integer) params.get("type");
                if (opType != null) {
                    JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
                    switch (opType) {
                        case 1: {
                            //all publish;
                            rowNum = jt.update("update book_document set approve_status = ? where approve_status = ? or approve_status = ? ", new Object[]{ConstValueUtils.APPROVE_PUBLISH, ConstValueUtils.APPROVE_RECEIVE_SUCCESS, ConstValueUtils.APPROVE_HAS_UPDATE});
                            logger.info("publish num:" + rowNum);
                            break;
                        }
                        case 2: {
                            //publish
                            List<Integer> idList = (List<Integer>) params.get("idList");
                            if (idList != null && idList.size() > 0) {
                                StringBuffer idStr = new StringBuffer("0");
                                for (Integer idN : idList) {
                                    idStr.append(",").append(idN);
                                }
                                rowNum = jt.update("update book_document set approve_status = ? where oid in (" + idStr.toString() + ")", new Object[]{ConstValueUtils.APPROVE_PUBLISH});
                                logger.info("publish num:" + rowNum);
                            }
                            break;
                        }
                        case 3: {
                            //reject
                            List<Integer> idList = (List<Integer>) params.get("idList");
                            if (idList != null && idList.size() > 0) {
                                StringBuffer idStr = new StringBuffer("0");
                                for (Integer idN : idList) {
                                    idStr.append(",").append(idN);
                                }
                                rowNum = jt.update("update book_document set approve_status = ? where oid in (" + idStr.toString() + ")", new Object[]{ConstValueUtils.APPROVE_REJECT});
                                logger.info("reject num:" + rowNum);
                            }
                            break;
                        }
                    }
                    cr.setCode(CommandResult.CODE_SUCCESS);
                }
            }
        } catch (Exception ex) {
            logger.error("publish error!", ex);
            cr.setMessage(ex.getMessage());
        } finally {
            Map<String,Object> data = new HashMap<String, Object>();
            data.put("data",rowNum);
            cr.setData(data);
        }
        return cr;
    }
}
